package com.zhenwei.zscm.common.base;

import lombok.Data;

@Data
public class RestResult {
    private int code;
    private String msg;
    private Object data;
    private long totalCount;
    private RestResult(int code, String msg, Object data, long totalCount) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.totalCount = totalCount;
    }

    private static int SUCCESS_CODE = 200;
    private static int ERROR_CODE = 500;
    private static int FAILURE_CODE = 400;
    private static int UNAUTHORIZED_CODE = 401;
    public static RestResult success() {
        return new RestResult(SUCCESS_CODE, RestMsg.SUCCESS, null, 0L);
    }
    public static RestResult success(Object data, long totalCount) {
        return new RestResult(SUCCESS_CODE, RestMsg.SUCCESS, data, totalCount);
    }
    public static RestResult success(String msg, Object data, long totalCount) {
        return new RestResult(SUCCESS_CODE, msg, data, totalCount);
    }
    public static RestResult success(String msg, Object data) {
        return new RestResult(SUCCESS_CODE, msg, data, 0L);
    }
    public static RestResult fail(String msg){
        return new RestResult(FAILURE_CODE, msg, null,0L);
    }
    public static RestResult fail(){
        return new RestResult(FAILURE_CODE, RestMsg.FAILURE, null,0L);
    }
    public static RestResult error(String msg){
        return new RestResult(ERROR_CODE, msg, null,0L);
    }
    public static RestResult unauthorized(String msg){
        return new RestResult(UNAUTHORIZED_CODE, msg, null,0L);
    }
    public static RestResult r(boolean predicate){
        return predicate ? success() : fail();
    }

}
